<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('notifications', function (Blueprint $table) {
            $table->id();
            $table->string('type', 100)->comment('通知类型');
            $table->string('title', 200)->comment('通知标题');
            $table->text('content')->comment('通知内容');
            $table->json('data')->nullable()->comment('通知数据');
            $table->enum('level', ['info', 'warning', 'error', 'success'])->default('info')->comment('通知级别');
            $table->enum('status', ['unread', 'read', 'archived'])->default('unread')->comment('通知状态');
            $table->unsignedBigInteger('sender_id')->nullable()->comment('发送者ID');
            $table->unsignedBigInteger('recipient_id')->comment('接收者ID');
            $table->timestamp('read_at')->nullable()->comment('阅读时间');
            $table->timestamp('expires_at')->nullable()->comment('过期时间');
            $table->timestamps();
            
            // 外键约束
            $table->foreign('sender_id')->references('id')->on('users')->onDelete('set null');
            $table->foreign('recipient_id')->references('id')->on('users')->onDelete('cascade');
            
            // 索引
            $table->index(['recipient_id', 'status']);
            $table->index(['type', 'status']);
            $table->index(['level', 'status']);
            $table->index('created_at');
            $table->index('expires_at');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('notifications');
    }
};
